home *** CD-ROM | disk | FTP | other *** search
/ Language/OS - Multiplatform Resource Library / LANGUAGE OS.iso / cpp_libs / csim / source.lha / source / Threads / GnuThreads / sem.c < prev    next >
Encoding:
C/C++ Source or Header  |  1993-06-14  |  1.6 KB  |  69 lines

  1. /*
  2.  * sem.c -- semaphore manipulation.
  3.  * Copyright (C) 1991 Stephen Crane.
  4.  *
  5.  * This is free software; you can redistribute it and/or modify
  6.  * it under the terms of the GNU General Public License as published by
  7.  * the Free Software Foundation; either version 1, or (at your option)
  8.  * any later version.
  9.  *
  10.  * This software is distributed in the hope that it will be useful,
  11.  * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12.  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  13.  * GNU General Public License for more details.
  14.  *
  15.  * You should have received a copy of the GNU General Public License
  16.  * see the file COPYING.  If not, write to
  17.  * the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.
  18.  *
  19.  * author: Stephen Crane, (jsc@doc.ic.ac.uk), Department of Computing,
  20.  * Imperial College of Science, Technology and Medicine, 180 Queen's
  21.  * Gate, London SW7 2BZ, England.
  22.  */
  23.  
  24. #include <malloc.h>
  25. #include "gnulwp.h"
  26.  
  27. /*
  28.  * creats -- create a semaphore.
  29.  */
  30. struct sem *creats (int count)
  31. {
  32.     struct sem *new_s;
  33.  
  34.     if (!(new_s = (struct sem *)malloc (sizeof(struct sem))))
  35.         return (0);
  36.     new_s->count = count;
  37.     new_s->q.head = new_s->q.tail = 0;
  38.     return (new_s);
  39. }
  40.  
  41. /*
  42.  * signals -- signal a semaphore.  We only yield here if
  43.  * the blocked process has a higher priority than ours'.
  44.  */
  45. void signals (struct sem *s)
  46. {
  47.     extern struct pcb *currp;
  48.  
  49.     if (s->count++ < 0) {
  50.         struct pcb *p = hoq (&s->q);
  51.         readyp (p);
  52.         if (currp->pri < p->pri)
  53.             yieldp ();
  54.     }
  55. }
  56.  
  57. /*
  58.  * waits -- wait on a semaphore
  59.  */
  60. void waits (struct sem *s)
  61. {
  62.     extern struct pcb *currp;
  63.  
  64.     if (--s->count < 0) {
  65.         toq (&s->q, currp);
  66.         reschedp ();
  67.     }
  68. }
  69.